package com.fzh.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fzh.mapper.CartMapper;
import com.fzh.pojo.entity.Cart;
import com.fzh.pojo.utils.ResponeMessageUtil;
import com.fzh.service.CartService;

@Service
public class CartServiceImpl implements CartService {
	
	@Autowired
	private CartMapper cartMapper;

	@Override
	public List<Cart> queryAllCart(Integer setValue,Integer limit,String f_uid,String f_pid) {
		return cartMapper.selectAllCart(setValue,limit,f_uid,f_pid);
	}

	@Override
	public Integer queryCartCounts(String f_uid,String f_pid) {
		return cartMapper.selectCounts(f_uid,f_pid);
	}

	@Override
	public Boolean addCart(Cart cart) {
		return ResponeMessageUtil.getResMsg(cartMapper.insertCart(cart));
	}

	@Override
	public Boolean editCartById(Cart cart) {
		return ResponeMessageUtil.getResMsg(cartMapper.updateCartById(cart));
	}

	@Override
	public Boolean delAllCartByIds(String id) {
		String[] split = id.split(",");
		List<String> ids = new ArrayList<>();
		for(int i = 0; i < split.length; i ++) {
			ids.add(split[i]);
		}
		return ResponeMessageUtil.getResMsg(cartMapper.deleteAllCartByIds(ids));
	}

	@Override
	public Boolean delCartById(Integer id) {
		return ResponeMessageUtil.getResMsg(cartMapper.deleteCartById(id));
	}

	@Override
	public List<Cart> queryCartByUserIdF(Integer id) {
		return cartMapper.selectCartByUserIdF(id);
	}

	@Override
	public String queryCartCountByUserIdF(Integer id) {
		return cartMapper.selectCartCountByUserIdF(id);
	}

	@Override
	public String queryCartTotalSumByUserIdF(Integer id) {
		return cartMapper.selectCartTotalSumByUserIdF(id);
	}

	@Override
	public Boolean delCartProductByUserIdAndProductIdF(Integer productId, Integer userId) {
		return ResponeMessageUtil.getResMsg(cartMapper.deleteCartProductByUserIdAndProductIdF(productId,userId));
	}

	@Override
	public Boolean addCartByUserIdAndProductIdF(Cart cart) {
		return ResponeMessageUtil.getResMsg(cartMapper.insertCartByUserIdAndProductIdF(cart));
	}

	@Override
	public Cart queryCartByUserIdAndProductIdF(Integer userId, Integer productId) {
		return cartMapper.selectCartByUserIdAndProductIdF(userId,productId);
	}

	@Override
	public List<Cart> queryCartByUserIdAndProductIds(Integer userId, List<String> ids) {
		//根据商品id查询出对应购物车
		List<Cart> carts = cartMapper.selectCartByUserIdAndProductIds(ids);
		List<Cart> cart = new ArrayList<Cart>();
		for(int i = 0; i < carts.size(); i ++) {
			//通过判断进一步根据用户id进行处理购物车
			if(carts.get(i).getF_uid() == userId) {
				cart.add(carts.get(i));
			}
		}
		return cart;
	}

	@Override
	public void removeCartByUserId(Integer userId) {
		cartMapper.deleteCartByUserId(userId);
	}

	/**
	 * {前端}根据选中得购物车id查询
	 * @param userId
	 */
	public List<Cart> selectCartByIds(String[] ids){
		return cartMapper.selectCartByIds(ids);
	};
}
